﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DataAccessLayer.Models
{
    public class CodedObjectLoader
    {
        private static LOSEntities db = new LOSEntities();

        private static CodedObjectLoader loader = null;

        private static List<Role> roles = null;
        private static List<Product> products = null;
        private static List<Country> countries = null;

        public static CodedObjectLoader getInstance()
        {
            if (loader == null) loader = new CodedObjectLoader();
            return loader;
        }

        public List<Role> getRoles()
        {
            var ids = from c in db.Roles select c;
            if (roles == null) roles = new List<Role>(ids);
            return roles;
        }

        public List<Product> getProducts()
        {
            if (products == null) products = db.Products.ToList<Product>();
            return products;
        }

        public List<Country> getCountries()
        {
            if (countries == null) countries = db.Countries.ToList<Country>();
            return countries;
        }

        public Boolean isPreparer(string roleId)
        {
            foreach (Role r in getRoles())
            {
                if (r.RoleId.Equals(roleId) && r is Preparer) return true;
            }

            return false;
        }

        public Boolean isVerifier(string roleId)
        {
            foreach (Role r in getRoles())
            {
                if (r.RoleId.Equals(roleId) && r is Verifier) return true;
            }

            return false;
        }

        public Boolean isApprover(string roleId)
        {
            foreach (Role r in getRoles())
            {
                if (r.RoleId.Equals(roleId) && r is Approver) return true;
            }

            return false;
        }

        public Boolean isReviewer(string roleId)
        {
            foreach (Role r in getRoles())
            {
                if (r.RoleId.Equals(roleId) && r is Reviewer) return true;
            }

            return false;
        }

        public string getRoleName(string roleId)
        {
            foreach(Role r in getRoles())
            {
                if (r.RoleId.Equals(roleId)) return r.RoleName;
            }

            return null;
        }
    }
}
